﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;
using System.Data;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using System.Globalization;
using System.Text;
using System.Data.OleDb;
using System.Collections;
public partial class Admin_Sys_Dic_Dic_ThirdUnit_SecondFloorAdd : AdminBasePage
{
    protected Guid badd = new Guid();
    protected Guid tunitID = new Guid();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[BFThirdUnitNumberMetadata.ColumnNames.ThirdUnitNumberID] != null)
        {
            badd = Guid.Parse(Request.QueryString[BFThirdUnitNumberMetadata.ColumnNames.ThirdUnitNumberID]);
        }
        if (Request.QueryString["id"] != null) {
            tunitID = Guid.Parse(Request.QueryString["id"]);
        }
        if (!IsPostBack)
        {
            switch (ActionType)
            {
                case OperationType.Add:
                    btnSave.Text = "新增";
                    radActive.Checked = true;
                    break;
                case OperationType.Update:
                    if (badd != null)
                    {
                        BFThirdUnitNumber bfcabin = new BFThirdUnitNumber();
                        if (bfcabin.LoadByPrimaryKey(badd))
                        {
                            txtName.Text = bfcabin.Name;                         
                            if (bfcabin.Status.HasValue && bfcabin.Status.Value == (int)ActiveStatus.Active)
                            {
                                radActive.Checked = true;
                            }
                            else
                            {
                                radActive.Checked = true;
                            }
                        }
                        btnSave.Text = "修改";
                    }
                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e) {
        try
        {
            BFThirdUnitNumber bfcabin = new BFThirdUnitNumber();
            if (ActionType == OperationType.Add || ActionType == OperationType.Update)
            {
                bfcabin.Query.Where(bfcabin.Query.Name == txtName.Text.Trim());
                if (ActionType == OperationType.Add) {
                    bfcabin.Query.Where(bfcabin.Query.ThirdUnitCabinID == tunitID);
                    bfcabin.Query.Load();
                    if (bfcabin.es.HasData)
                    {
                        lbMessage.Text = "机构名称已存在。";
                        return;
                    }
                }
                if (ActionType == OperationType.Update) {
                    bfcabin.Query.Where(bfcabin.Query.ThirdUnitNumberID != badd);
                    bfcabin.Query.Load();
                    if (bfcabin.es.HasData) {
                        lbMessage.Text = "机构名称已存在。";
                        return;
                    }
                }                                 
            }
            switch (ActionType)
            {
                case OperationType.Add:
                    bfcabin.ThirdUnitCabinID = tunitID;                
                    bfcabin.Name = txtName.Text.Trim();
                    bfcabin.ThirdUnitNumberID = Guid.NewGuid();
                    bfcabin.CreatedBy = bfcabin.LastedUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    bfcabin.CreatedDate = bfcabin.LastedUpdatedDate = DateTime.Now;
                    bfcabin.Status = radActive.Checked ? 1 : 0;
                    bfcabin.Save();
                    break;
                case OperationType.Update:
                    bfcabin.LoadByPrimaryKey(badd);
                    bfcabin.Name = txtName.Text.Trim();
                    bfcabin.LastedUpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    bfcabin.LastedUpdatedDate = DateTime.Now;
                    bfcabin.Save();
                    break;
                case OperationType.Delete:
                    try
                    {
                        bfcabin.LoadByPrimaryKey(badd);
                        bfcabin.Query.Where(bfcabin.Query.ThirdUnitNumberID == badd);
                        bfcabin.Query.Load();
                        bfcabin.MarkAsDeleted();
                        bfcabin.Save();
                    }
                    catch (Exception ex)
                    {
                        WebHelper.GotoParent("无法删除已经使用过的凭证存放库");
                    }
                    break;
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex) {
            lbMessage.Text = ex.Message;
        }
    }
    
}